package com.penjin.android.message.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;

import com.easemob.chat.EMChatManager;
import com.easemob.chat.EMConversation;
import com.penjin.android.R;
import com.penjin.android.message.adapter.EaseConversationAdapater;

import android.content.Context;
import android.content.res.TypedArray;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Pair;
import android.widget.ListView;

public class EaseConversationList extends ListView {
	protected int primaryColor;
	protected int secondaryColor;
	protected int timeColor;
	protected int primarySize;
	protected int secondarySize;
	protected float timeSize;

	protected final int MSG_REFRESH_ADAPTER_DATA = 0;

	protected Context context;
	protected EaseConversationAdapater adapter;
	protected List<EMConversation> conversationList = new ArrayList<EMConversation>();

	public EaseConversationList(Context context, AttributeSet attrs) {
		super(context, attrs);
		init(context, attrs);
	}

	public EaseConversationList(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		init(context, attrs);
	}

	private void init(Context context, AttributeSet attrs) {
		this.context = context;
		TypedArray ta = context.obtainStyledAttributes(attrs,
				R.styleable.EaseConversationList);//如果没有制定这些属性，会为它提供一个默认值
		primaryColor = ta.getColor(
				R.styleable.EaseConversationList_cvsListPrimaryTextColor,
				R.color.list_itease_primary_color);
		secondaryColor = ta.getColor(
				R.styleable.EaseConversationList_cvsListSecondaryTextColor,
				R.color.list_itease_secondary_color);
		timeColor = ta.getColor(
				R.styleable.EaseConversationList_cvsListTimeTextColor,
				R.color.list_itease_secondary_color);
		primarySize = ta.getDimensionPixelSize(
				R.styleable.EaseConversationList_cvsListPrimaryTextSize, 0);
		secondarySize = ta.getDimensionPixelSize(
				R.styleable.EaseConversationList_cvsListSecondaryTextSize, 0);
		timeSize = ta.getDimension(
				R.styleable.EaseConversationList_cvsListTimeTextSize, 0);
		ta.recycle();

	}

	public void init(List<EMConversation> conversationList) {
		this.conversationList = conversationList;
		adapter = new EaseConversationAdapater(context, 0, conversationList);
		adapter.setPrimaryColor(primaryColor);
		adapter.setPrimarySize(primarySize);
		adapter.setSecondaryColor(secondaryColor);
		adapter.setSecondarySize(secondarySize);
		adapter.setTimeColor(timeColor);
		adapter.setTimeSize(timeSize);
		setAdapter(adapter);
	}

	Handler handler = new Handler() {
		@Override
		public void handleMessage(Message message) {
			switch (message.what) {
			case MSG_REFRESH_ADAPTER_DATA:
				if (adapter != null) {
					conversationList.clear();
					conversationList.addAll(loadConversationsWithRecentChat());
					adapter.notifyDataSetChanged();
				}
				break;
			default:
				break;
			}
		}
	};

	/**
	 * 获取所有会话
	 * 
	 * @param context
	 * @return +
	 */
	private List<EMConversation> loadConversationsWithRecentChat() {
		// 获取所有会话，包括陌生人
		Map<String, EMConversation> conversations = EMChatManager.getInstance()
				.getAllConversations();
		// 过滤掉messages size为0的conversation
		/**
		 * 如果在排序过程中有新消息收到，lastMsgTime会发生变化 影响排序过程，Collection.sort会产生异常
		 * 保证Conversation在Sort过程中最后一条消息的时间不变 避免并发问题
		 */
		List<Pair<Long, EMConversation>> sortList = new ArrayList<Pair<Long, EMConversation>>();
		synchronized (conversations) {
			for (EMConversation conversation : conversations.values()) {
				if (conversation.getAllMessages().size() != 0) {
					sortList.add(new Pair<Long, EMConversation>(conversation
							.getLastMessage().getMsgTime(), conversation));
				}
			}
		}
		try {
			// Internal is TimSort algorithm, has bug
			sortConversationByLastChatTime(sortList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		List<EMConversation> list = new ArrayList<EMConversation>();
		for (Pair<Long, EMConversation> sortItem : sortList) {
			list.add(sortItem.second);
		}
		return list;
	}

	/**
	 * 根据最后一条消息的时间排序
	 * 
	 * @param usernames
	 */
	private void sortConversationByLastChatTime(
			List<Pair<Long, EMConversation>> conversationList) {
		Collections.sort(conversationList,
				new Comparator<Pair<Long, EMConversation>>() {
					@Override
					public int compare(final Pair<Long, EMConversation> con1,
							final Pair<Long, EMConversation> con2) {

						if (con1.first == con2.first) {
							return 0;
						} else if (con2.first > con1.first) {
							return 1;
						} else {
							return -1;
						}
					}

				});
	}

	public EMConversation getItem(int position) {
		return (EMConversation) adapter.getItem(position);
	}

	public void refresh() {
		conversationList = loadConversationsWithRecentChat();

		handler.sendEmptyMessage(MSG_REFRESH_ADAPTER_DATA);
	}

	public void filter(CharSequence str) {
		adapter.getFilter().filter(str);
	}
}
